﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.CMS263.DAO;
using Com.CMS263.PO;
namespace Com.CMS263.BLL
{
  
    public class ExtAttributeBLL:BaseBLL <ExtAttribute >
    {
        ExtAttributeDAO eaDAO = new ExtAttributeDAO();
        ExtValueDAO exDao = new ExtValueDAO();
        public ExtAttribute entity = new ExtAttribute();
        public override ExtAttribute GetModel()
        {
            return entity;
        }
        public void Add(ExtAttribute ea)
        {
            eaDAO.Add(ea);
        }
        public IList<ExtAttribute> GetExtAttributeByTypeId(int typeId)
        {
            return  eaDAO.GetExtAttributeByTypeId(typeId);
        }
        public IList<ExtAttribute> GetShowInBackAttrByTypeId(int typeId)
        {
            return eaDAO.GetShowInBackAttrByTypeId(typeId);
        }
        
        public string ShowAttr(ExtAttribute EA)
        {
            string html = "";
            string note = "";
            if (EA.notNull == 1)
            {
                note = "(此为必填项)";
            }
            if (EA.attrType == 1)
            {
                html = "<input type='text' name='" + EA.attrName + "' id='" + EA.attrName + "' value='" + (string.IsNullOrEmpty(EA.defaultValue) ? "" : EA.defaultValue) + "' />";
            }
            else if (EA.attrType == 2)
            {
                html = " <textarea  name='" + EA.attrName + "' id='" + EA.attrName + "' cols='45' rows='5'>" + (string.IsNullOrEmpty(EA.defaultValue) ? "" : EA.defaultValue) + "</textarea>";
            }

            else if (EA.attrType == 3)
            {
                html += "<span style='color:red'>如果是从word文档粘贴的内容,请选择清除或过滤样式</span><input type='hidden' id='" + EA.attrName + "' name='" + EA.attrName + "' value='' /><input type='hidden' id='" + EA.attrName + "___Config' value='HtmlEncodeOutput=false' /><iframe id='" + EA.attrName + "___Frame' src='/fckeditor/editor/fckeditor.html?InstanceName=" + EA.attrName + "&amp;Toolbar=Default' width='100%' height='250px' frameborder='no' scrolling='no'></iframe>";
            }
            else if (EA.attrType == 4)
            {
                html += " <input type='file' name='" + EA.attrName + "' id='" + EA.attrName + "' />";
            }
            else if (EA.attrType == 5)
            {
                html += " <input type='file' name='" + EA.attrName + "' id='" + EA.attrName + "' />";
            }
            else if (EA.attrType == 8)
            {
                html = "<select name='" + EA.attrName + "' id='" + EA.attrName + "'>";
                string[] arr = EA.attrValue.Split(' ');
                for (int i = 0; i < arr.Length; i++)
                {
                    if (arr[i].Length > 0)
                    {
                        html += " <option value='" + arr[i] + "'>" + arr[i] + "</option>";
                    }
                }
                html += "</select>";
            }
            else if (EA.attrType == 6)
            {
                string[] arr = EA.attrValue.Split(' ');
                for (int i = 0; i < arr.Length; i++)
                {
                    if (arr[i].Length > 0)
                    {
                        if (i == 0)
                        {
                            html += "<input type='radio' name='" + EA.attrName + "' checked='checked' value='" + arr[i] + "'>" + arr[i];
                        }
                        else
                        {
                            html += "<input type='radio' name='" + EA.attrName + "' value='" + arr[i] + "'>" + arr[i];
                        }
                    }
                }

            }
            else if (EA.attrType == 7)
            {
                string[] arr = EA.attrValue.Split(' ');
                for (int i = 0; i < arr.Length; i++)
                {
                    if (arr[i].Length > 0)
                    {
                        html += "<input type='checkbox' name='" + EA.attrName + "' value='" + arr[i] + "'>" + arr[i];
                    }
                }
            }
            return html + "&nbsp;&nbsp;&nbsp;&nbsp;" + note;
        }


        public string ShowAttr(ExtAttribute EA, int id)
        {
            string html = "";
            string note = "";
            if (EA.notNull == 1)
            {
                note = "(此为必填项)";
            }
            if (EA.attrType == 1)
            {
                html = "<input type='text' name='" + EA.attrName + "' id='" + EA.attrName + "' value='" + (exDao.GetValueById(id, EA.id)) + "' />";
            }
            else if (EA.attrType == 2)
            {
                html = " <textarea  name='" + EA.attrName + "' id='" + EA.attrName + "' cols='45' rows='5'>" + (exDao.GetValueById(id, EA.id)) + "</textarea>";
            }

            else if (EA.attrType == 3)
            {
                html += "<span style='color:red'>如果是从word文档粘贴的内容,请选择清除或过滤样式</span><input type='hidden' id='" + EA.attrName + "' name='" + EA.attrName + "' value='" + (string.IsNullOrEmpty(exDao.GetValueById(id, EA.id)) ? "" : exDao.GetValueById(id, EA.id).Replace("'", "’") )+ "' /><input type='hidden' id='" + EA.attrName + "___Config' value='HtmlEncodeOutput=false' /><iframe id='" + EA.attrName + "___Frame' src='/fckeditor/editor/fckeditor.html?InstanceName=" + EA.attrName + "&amp;Toolbar=Default' width='100%' height='250px' frameborder='no' scrolling='no'></iframe>";
            }
            else if (EA.attrType == 4)
            {
                html += " <input type='file' name='" + EA.attrName + "' id='" + EA.attrName + "' />" + "<br/><img src='/ArtUpFile/" + (exDao.GetValueById(id, EA.id)) + "' style='width:200px' / >";
            }
            else if (EA.attrType == 5)
            {
                html += " <input type='file' name='" + EA.attrName + "' id='" + EA.attrName + "' />" + "<a href='/ArtUpFile/" + (exDao.GetValueById(id, EA.id)) + "' target='_blank' >查看文件</a>";
            }
            else if (EA.attrType == 8)
            {
                html = "<select name='" + EA.attrName + "' id='" + EA.attrName + "'>";
                string[] arr = EA.attrValue.Split(' ');
                for (int i = 0; i < arr.Length; i++)
                {
                    if (arr[i].Length > 0)
                    {
                        if (arr[i] == exDao.GetValueById(id, EA.id))
                        {
                            html += " <option selected='selected' value='" + arr[i] + "'>" + arr[i] + "</option>";
                        }
                        else
                        {
                            html += " <option value='" + arr[i] + "'>" + arr[i] + "</option>";
                        }
                    }
                }
                html += "</select>";
            }
            else if (EA.attrType == 6)
            {
                string[] arr = EA.attrValue.Split(' ');
                string val = exDao.GetValueById(id, EA.id);
                for (int i = 0; i < arr.Length; i++)
                {
                    if (arr[i].Length > 0)
                    {
                        if (arr[i] == val)
                        {
                            html += "<input type='radio' name='" + EA.attrName + "' checked='checked' value='" + arr[i] + "'>" + arr[i];
                        }
                        else
                        {
                            html += "<input type='radio' name='" + EA.attrName + "' value='" + arr[i] + "'>" + arr[i];
                        }
                    }
                }

            }
            else if (EA.attrType == 7)
            {
                string[] arr = EA.attrValue.Split(' ');
                string val = exDao.GetValueById(id, EA.id);
                for (int i = 0; i < arr.Length; i++)
                {
                    if (arr[i].Length > 0)
                    {
                        if (val.IndexOf(arr[i]) > 0)
                        {
                            html += "<input type='checkbox' checked='checked' name='" + EA.attrName + "' value='" + arr[i] + "'>" + arr[i];
                        }
                        else
                        {
                            html += "<input type='checkbox' name='" + EA.attrName + "' value='" + arr[i] + "'>" + arr[i];
                        }
                    }
                }
            }
            return html + "&nbsp;&nbsp;&nbsp;&nbsp;" + note;
        }

        public bool Check(int typeId, string attrName)
        {
            return eaDAO.Check(typeId,attrName);
        }
    }
}
